package com.bjpowernode.controller;

import com.bjpowernode.vo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MyController
 * @Description 当@RequestMapping中有公共的部分时，可以将公共部分放到类的上边
 * @Author hhwu
 * @Date 2022/3/10 13:04
 * Version 1.0
 **/

@Controller//创建处理器对象，和@Service,@Component一样
@RequestMapping("/user")//当方法上的@RequestMapping中有公共的部分时，可以将公共部分抽取放到类的上边，更加方便易辨别
public class MyController {
    @RequestMapping(value = {"/returnString.do"})//不指定请求方式时没有限制
    public String doReceiveObject(HttpServletRequest request, Student student) {
        /**
         * @Author hhwu
         * @Description //TODO 处理器方法返回string即show，表示逻辑视图名称，需要在springmvc.xml配置视图解析器
         * 如果直接返回完整视图路径，则不能配置视图解析器，否则会有重复路径
         * @Date 9:23 2022/3/11
         * @Param [name, age]
         * @return org.springframework.web.servlet.ModelAndView
         **/
        //使用request域也可以接收参数，手动添加
        request.setAttribute("myname", student.getName());
        request.setAttribute("myage", student.getAge());
        System.out.println("name:"+student.getName()+" age:"+student.getAge());
        return "show";
    }

    @ResponseBody//将处理器返回的json，通过httpservletresponse输出给浏览器
    @RequestMapping(value = "/returnStudentJson.do")
    public Student doStudentJsonObject(String name, Integer age) {
        /**
         * @Author hhwu
         * @Description //TODO 处理器方法返回一个student对象，通过框架转为json，相应ajax请求，参考readme.txt，因为实现已经导入了依赖和注册了驱动
         * @Date 17:59 2022/3/11
         * @Param [name, age]
         * @return com.bjpowernode.vo.Student
         **/
        //模拟获取到的student数据
        Student student = new Student();
        student.setName("李四");
        student.setAge(4);
        return student;//会被框架自动转为json
    }

    @ResponseBody//将处理器返回的json，通过httpservletresponse输出给浏览器
    @RequestMapping(value = "/returnStudentJsonArray.do")
    public List<Student> doStudentJsonObjectArray (String name, Integer age) {
        /**
         * @Author hhwu
         * @Description //TODO 处理器方法返回一个student集合
         * @Date 17:59 2022/3/11
         * @Param [name, age]
         * @return com.bjpowernode.vo.Student
         **/
        //模拟获取到的student数据
        ArrayList<Student> list = new ArrayList<>();
        Student student = new Student();
        student.setName("李四");
        student.setAge(4);
        list.add(student);

        Student student1 = new Student();
        student1.setName("张三");
        student1.setAge(3);
        list.add(student1);

        return list;
    }

    @ResponseBody
    //设置字符串的编码方式和过滤器的编码方式一致，否则乱码
    @RequestMapping(value = "/returnStringData.do", produces = "text/plain;charset=UTF-8")
    public String doStringData(String name, Integer age) {
        return "返回对象，表示数据";
    }
}
